<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="关键词" prop="search">
        <el-input
          v-model="queryParams.search"
          placeholder="请输入关键词"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户等级" prop="userLevel">
        <el-select v-model="queryParams.userLevel" placeholder="请选择">
          <el-option
            v-for="item in levelList"
            :key="item.id"
            :label="item.level"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户资料" prop="isInformation">
        <el-select v-model="queryParams.isInformation" placeholder="请选择">
          <el-option label="全部" value=""></el-option>
          <el-option label="已填写" value="1"></el-option>
          <el-option label="未填写" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="头像" align="center" width="150">
        <template slot-scope="scope">
          <el-avatar :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="用户等级" align="center" width="150px">
        <template slot-scope="scope">
          <span>{{ getLevelFormat(scope.row.userLevel) }}</span> &nbsp;&nbsp;
          <el-button icon="el-icon-edit" size="mini" type="info" circle @click="handleUpdate(scope.row, '等级')"></el-button>
        </template>
      </el-table-column>
      <el-table-column label="用户资料" align="left" prop="phone" width="200">
        <template slot-scope="scope">
          <div style="font-size: 12px; line-height: 1.5;">
          真实姓名: {{ scope.row.realname || '-' }} <br>
          手机号: {{ scope.row.phone || '-' }} <br>
          公司: {{ scope.row.company_name || '-' }} <br>
          职务: {{ scope.row.position || '-' }} <br>
          邮箱: {{ scope.row.email || '-' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="奶酪/水滴" align="center" width="200">
        <template slot-scope="scope">
          <div style="font-size: 12px; line-height: 3;">
            奶酪：<el-tag type="primary" size="mini">{{ scope.row.cheese || '0.00' }}</el-tag> &nbsp;&nbsp;
            <el-button icon="el-icon-plus" size="mini" type="primary" circle  @click="handleUpdate(scope.row, '奶酪')"></el-button>
            <br>
            水滴：<el-tag type="primary" size="mini">{{ scope.row.integral || '0.00' }}</el-tag> &nbsp;&nbsp;
            <el-button icon="el-icon-plus" size="mini" type="primary" circle  @click="handleUpdate(scope.row, '水滴')"></el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否管理员" align="center" prop="isAdmin">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isAdmin" :active-value="1" :inactive-value="0" @change="handleStatusChange(scope.row, 'isAdmin')"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户" prop="nickname">
          {{ form.nickname }}
        </el-form-item>
        <template v-if="dialogType == '等级'">
          <el-form-item label="用户等级" prop="userLevel">
            <el-select v-model="form.userLevel" placeholder="请选择">
              <el-option
                v-for="item in levelList"
                :key="item.id"
                :label="item.level"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </template>
        <template v-if="dialogType == '奶酪'">
          <el-form-item label="奶酪余额" prop="cheese">
            <el-input v-model="form.cheese" placeholder="请输入奶酪余额" />
          </el-form-item>
        </template>
        <template v-if="dialogType == '水滴'">
          <el-form-item label="水滴余额" prop="integral">
            <el-input v-model="form.integral" placeholder="请输入水滴余额" />
          </el-form-item>
        </template>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/mobile/user"
import { listLevel } from "@/api/mobile/level"

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        search: null,
        userLevel: null,
        isInformation: null
      },
      levelList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      dialogType: 'type'
    }
  },
  created() {
    listLevel(this.queryParams).then(response => {
      this.levelList = response.rows
    });
    this.getList()
  },
  methods: {
    getLevelFormat(status) {
      const dart = this.levelList.find(item => item.id === status);
      if (dart) {
        return dart.level;
      } else {
        return '未知';
      }
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      listUser(this.queryParams).then(response => {
        this.userList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        openid: null,
        avatar: null,
        nickname: null,
        realname: null,
        userLevel: null,
        cheese: null,
        integral: null,
        phone: null,
        lawyerJoblevel: null,
        isAdmin: null,
        sort: null,
        status: null,
        createdAt: null,
        updatedAt: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加【请填写功能名称】"
    },
    /** 修改按钮操作 */
    handleUpdate(row, type='等级') {
      this.dialogType = type;
      this.reset()
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data
        this.open = true
        if(type == '等级'){
        this.title = `修改${type}`
        }else {
          this.title = `${type}操作`
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function() {
        return delUser(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },
    // 状态修改
    handleStatusChange(row, field='status') {
      let text = row[field] == 0 ? "停用" : "启用"
      this.$modal.confirm(`确认要"${text}"${row.title}吗？`).then(function() {
        return updateUser({id :row.id, [field]: row[field]})
      }).then(() => {
        this.$modal.msgSuccess(text + "成功")
      }).catch(function() {
        row[field] = row[field] == "0" ? 1 : 0
      })
    },
  }
}
</script>
